/**
 * 
 */
package nz.ac.massey.cs.se400_2013.ass3.gui;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.InputStream;
import java.util.Collection;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import nz.ac.massey.cs.se400_2013.ass3.message.Message;
import nz.ac.massey.cs.se400_2013.ass3.message.MessageHandler;
import nz.ac.massey.cs.se400_2013.ass3.message.MessageListener;
import nz.ac.massey.cs.se400_2013.ass3.message.MessageProducer;
import nz.ac.massey.cs.se400_2013.ass3.message.MessageType;

/**
 * @author Chen Here is a sidelist for the GUI to display different models.
 */
public class SideTree extends JPanel implements MessageProducer {
	/**
	 * @serial generated
	 */
	private static final long serialVersionUID = -5711305053173683697L;

	private DefaultTreeModel treeModel;
	private JScrollPane scrollPane;
	private JTree tree;
	private Collection collection;
	private MessageHandler messageHandler = new MessageHandler();

	public final static String NODE_Root = "Root";
	public final static String NODE_Courses = "Courses";
	public final static String NODE_Course = "Course";
	public final static String NODE_Papers = "Papers";
	public final static String NODE_Paper = "Paper";
	public final static String NODE_Students = "Students";
	public final static String NODE_Student = "Student";
	public final static String NODE_Address = "Address";

	DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(NODE_Root);
	DefaultMutableTreeNode courses = new DefaultMutableTreeNode(NODE_Courses);
	DefaultMutableTreeNode course = new DefaultMutableTreeNode(NODE_Course);
	DefaultMutableTreeNode papers = new DefaultMutableTreeNode(NODE_Papers);
	DefaultMutableTreeNode paper = new DefaultMutableTreeNode(NODE_Paper);
	DefaultMutableTreeNode students = new DefaultMutableTreeNode(NODE_Students);
	DefaultMutableTreeNode student = new DefaultMutableTreeNode(NODE_Student);
	DefaultMutableTreeNode address = new DefaultMutableTreeNode(NODE_Address);

	public SideTree() {
		super();
		setSize(300, 300);
		setLayout(new BorderLayout(0, 0));
		initTree();
		initClickEventListenr();
	}

	// Initialization method of SideTree
	private void initTree() {

		rootNode.add(courses);
		rootNode.add(papers);
		rootNode.add(students);
		
		courses.add(course);
		papers.add(paper);
		students.add(student);

		treeModel = new DefaultTreeModel(rootNode);
		tree = new JTree(treeModel);
		scrollPane = new JScrollPane();
		scrollPane.getViewport().add(tree, null);

		this.add(scrollPane);

	}

	// Initialization method of SideTree Click Event Listener
	// The TreeSelectionListener will only response for the first click on a node.
	private void initClickEventListenr() {

		// Left mouse click event listener
		this.tree.addTreeSelectionListener(new TreeSelectionListener() {

			@Override
			public void valueChanged(TreeSelectionEvent e) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
				// Publish the clicked node name for other components.
				sendMessage(new Message(MessageType.LEFT_CLICK, node.toString()));
				System.out.println("MessageType:" + MessageType.LEFT_CLICK + "MessageType:" + node.toString());
			}
		});

		// Using a mouse event listener to do
		// this.tree.addMouseListener(new MouseAdapter() {
		// public void mouseClicked(MouseEvent e) {
		// System.out.println(e.getSource().equals(tree));
		// if (e.getSource() == tree) {
		// TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
		// DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
		//
		// System.out.println(node.toString());
		// }
		// }
		// });
	}

	public <T> void render(Collection<T> collection) {

	}

	public static void main(String[] args) {

		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setSize(300, 300);
		frame.add(new SideTree());

		frame.setVisible(true);

	}

	@Override
	public void addMessageListener(MessageListener listener) {
		messageHandler.addListener(listener);
	}

	@Override
	public void removeMessageListener(MessageListener listener) {
		messageHandler.removeListener(listener);
	}

	@Override
	public void sendMessage(Message message) {
		messageHandler.sendMessage(message);
	}

}
